﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop;
using System.IO;
using ETADTools.Lib;

namespace ETADTools
{
  public partial class ETADTools : Form
  {
    private static string UpdaterPath = @"\\Hcm-tps-fp01-w\Share\Customers\Yara108\Updater\";

    public ETADTools()
    {
      InitializeComponent();

      InitControls();

      btnGenerate_Click(null, null);
    }

    private void InitControls()
    {
      txtUpdaterPath.Text = UpdaterPath;
    }


    private void btnBrowse_Click(object sender, EventArgs e)
    {
      using(FolderBrowserDialog folderDialog = new FolderBrowserDialog())
      {
        if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
          txtUpdaterPath.Text = folderDialog.SelectedPath;
        }
      }
    }

    private void btnGenerate_Click(object sender, EventArgs e)
    {
      string tableName = "tEmpPayrollInfo";
      string pageName = "EmpPayrollInfo";
      string author = "CNH";

      FieldList fieldList = new FieldList() { 
        new Field("PayrollCountry1", new IntType(), "1st Payroll Country"),
        new Field("LocalPayroll1Field1", new NVarcharType(120), "Local Payroll 1 Information (1)"),
        new Field("LocalPayroll1Field2", new NVarcharType(120), "Local Payroll 1 Information (2)"),
        new Field("PayrollCountry2", new IntType(), "2nd Payroll Country"),
        new Field("LocalPayroll2Field1", new NVarcharType(120), "Local Payroll 2 Information (1)"),
        new Field("LocalPayroll2Field2", new NVarcharType(120), "Local Payroll 2 Information (2)")
      };

      FormBuilder formBuilder2 = new SingleFormBuilder(
        new FormInfo(
          recordType: RecordType.Person,
          tableName: tableName,
          fieldList: fieldList,
          pageName: pageName,
          author: author,
          updaterPath: UpdaterPath
          )
        );

      //RecordType recordType = RecordType.EmpPerformance;
      //string tableName = "tEmpPerfPlanEvalOveral2";
      //string pageName = "EmpPerfPlanEvalOveral2";
      //string author = "CNH";

      //FieldList fieldList = new FieldList() { 
      //  new Field("IsUpToDate", new Bit(), "Is the position definition up to date?"),
      //  new Field("RScaleID", new Int(), "Rating scale"),
      //  new Field("Comment", new NText(), "Comments")
      //};

      //FormBuilder formBuilder = new SingleFormBuilder(
      //  recordType: recordType,
      //  tableName: tableName,
      //  fieldList: fieldList,
      //  pageName: pageName,
      //  author: author
      //);

      //rtbResult.Text = formBuilder2.Build();
      formBuilder2.Build();

      //
      // Build export script
      //

      //  string path = @"d:\Projects\ETWeb App Dev\Tasks\Fabory\Management\Spec\Fabory0005\Export specification Fabory - Safeguard_OneToMany_V1.0.xlsx";

      //  string fileName = string.Empty;
      //  int id = 01;

      //  string interfaceName = string.Empty;
      //  Field field = null;
      //  string fieldName = string.Empty;
      //  string fieldType = string.Empty;
      //  FieldList fieldList = new FieldList();

      //  Microsoft.Office.Interop.Excel.Application excelObj = new Microsoft.Office.Interop.Excel.Application();
      //  Microsoft.Office.Interop.Excel.Workbook theWorkbook = excelObj.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
      //                                                                                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
      //                                                                                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
      //                                                                                Type.Missing, Type.Missing);

      //  Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;

      //  Microsoft.Office.Interop.Excel.Worksheet worksheet = null;

      //  // Walkthough all the sheets
      //  for (int i = 1; i < sheets.Count + 1; ++i)
      //  {
      //    // Get the current sheet
      //    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(i);

      //    // Get the sheet name as interface name and then map it to short code
      //    interfaceName = GetShortName(worksheet.Name);
      //    fieldList = new FieldList();

      //    // Read data from the sheet
      //    // We ingore row 1 (field title), start running from row 2
      //    for (int x = 2; ; x++)
      //    {
      //      // Read all data from column A to J
      //      Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + x.ToString(), "J" + x.ToString());
      //      System.Array values = (System.Array)range.Cells.get_Value(Type.Missing);

      //      if (string.IsNullOrEmpty((string)values.GetValue(1, 1)))
      //      {
      //        break;
      //      }

      //      // Field name is in column 2
      //      fieldName = Formatter.FormatFieldName((string)values.GetValue(1, 2));

      //      // Field type is in column 7
      //      fieldType = (string)values.GetValue(1, 7);
      //      field = CreateField(fieldName, fieldType);

      //      fieldList.Add(field);
      //    }

      //    ExportInterfaceBuilder scriptBuilder = new ExportInterfaceBuilder(
      //      interfaceName: interfaceName,
      //      fieldList: fieldList,
      //      author: author
      //    );

      //    fileName = string.Format("{0} - pExport{1}_Execute.sql", id, interfaceName);
      //    id++;

      //    using (StreamWriter writer = new StreamWriter(UpdaterPath + fileName))
      //    {
      //      writer.Write(scriptBuilder.Build(txtUpdaterPath.Text));
      //    }
      //  }

      //  //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelObj);
      //  //excelObj = null;
      //}



      //private Field CreateField(string fieldName, string fieldType)
      //{
      //  Field field = null;

      //  if (string.Compare(fieldType, "Datetime", true) == 0)
      //  {
      //    field = new Field(fieldName, new NVarchar(), "", 11);
      //  }
      //  else if (string.Compare(fieldType, "Bit", true) == 0)
      //  {
      //    field = new Field(fieldName, new NVarchar(), "", 3);
      //  }
      //  else if (string.Compare(fieldType, "Integer", true) == 0)
      //  {
      //    field = new Field(fieldName, new NVarchar(), "", 44);
      //  }
      //  else if (string.Compare(fieldType, "Decimal", true) == 0)
      //  {
      //    field = new Field(fieldName, new NVarchar(), "", 55);
      //  }
      //  else
      //  {
      //    string[] parts = fieldType.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
      //    field = new Field(fieldName, new NVarchar(), "", Convert.ToInt32(parts[1]));
      //  }

      //  return field;
      //}

      //private string GetShortName(string interfaceName)
      //{
      //  switch (interfaceName)
      //  {
      //    case "Organization new hires":
      //      return "New";

      //    case "Leavers":
      //      return "Leavers";

      //    case "All data (static)":
      //      return "AllStat";

      //    case "Salary":
      //      return "Sal";

      //    case "Dependants":
      //      return "Depend";

      //    case "Salary Component Type":
      //      return "SalComp";

      //    case "Garnishment":
      //      return "Garnis";

      //    case "Other Allowences":
      //      return "OAll";

      //    case "Other Perks":
      //      return "OPerk";

      //    case "Overtime":
      //      return "OVert";

      //    case "Pension":
      //      return "Pens";

      //    case "Saving Arrangements":
      //      return "SavAr";

      //    case "Special Bonus":
      //      return "SpecB";

      //    case "Healthcare":
      //      return "Health";

      //    case "Disability":
      //      return "Disab";

      //    case "Accident":
      //      return "Accid";

      //    case "Other Insurance":
      //      return "OInsur";

      //    case "Vacation":
      //      return "Vac";

      //    case "Sickness":
      //      return "Sick";

      //    case "Other Absence":
      //      return "OAbs";

      //    default:
      //      return "Unknown";
      //  }
      //}
    }
  }
}
